#include <string.h>

#include "internalftpserver.h"

typedef unsigned short ushort;

#define    BUFSIZE          (1500)
#define    PACKTYPE(x)       x?         "FILE":"COMMAND"
#define    ISLASTPACK(x)     x?         "LAST":"NOT LAST"


#ifdef _unitest_ftp_server_

#define STATUS(x)         !(x)?        "SUCCESS":"FAILUER"
#define LOGERR(x)         printf("Test function %30s  %10s\n",#x,STATUS(x));

/**************************************************Internal funcitons for testing****************************/





void    regression_test();
int     unitest_init();
int     unitest_finduser();
int     unitest_parser();
int     unitest_FTPClient();
int     unitest_FTPHeader();
int     unitest_Headrfunc();
int     unitest_NewUserLogIn();
int     unitest_NewPassword();
int     unitest_Bye();
int     unitest_Get();
int     unitest_FTPls();

int main()
{
    regression_test();
    return 0;
}

void regression_test()
{
    LOGERR(unitest_init());
    LOGERR(unitest_parser());
    LOGERR(unitest_FTPClient());
    LOGERR(unitest_FTPHeader());
    printf("\n**********************************************************************\n");
    LOGERR(unitest_NewUserLogIn());
    printf("\n**********************************************************************\n");
    LOGERR(unitest_NewPassword());
    printf("\n**********************************************************************\n");
    LOGERR(unitest_Bye());
    LOGERR(unitest_Get());
    printf("\n**********************************************************************\n");
    LOGERR(unitest_FTPls());


}



int  unitest_init()
{
    FTPServ * ft;
    ft = FTPServerNew("login",9734);
    return 0;
}

int unitest_parser()
{
    char     command[100]={0};
    char     argument[100]={0};
    char      promt[]={"user sasha"};
    FTPparser(promt,command,argument);

    if( ! strcmp("user",command) && ! strcmp(argument,"sasha"))
    {
        return 0;
    }
    return 1;
}

int unitest_FTPClient()
{

    FTPServ*    ft;
    int         idclient =10;
    FTPattr     atr;

    ft = FTPServerNew("login",9734);

    atr.m_ft = ft;
    atr.m_sv = ft->m_gs;
    NewClient(idclient,&atr);
    return 0;
}

/*
int unitest_FTPHeader()
{
    char*               mes="User serj";
    char                headbuf[BUFSIZE];

    FTPHead             header;

    memcpy(headbuf+sizeof(FTPHead),mes,strlen(mes)+1);
    FTPFillHeadr(headbuf,0,1,0,strlen(mes), COMND_M);
    FTPReadHeadr(headbuf,OUT &header);

    if (header.m_position == 0 && header.m_islast == 1 && header.m_errnum ==0 && header.m_datasize == strlen(mes) && header.m_iscommand == COMND_M)
    {
        return 0;
    }
    return 1;
}*


int unitest_NewUserLogIn()
{
    /*Produce the new ftp*
    char        headbuf[BUFSIZE];
    int         flag=0;
    char*       mes="User serj";
    size_t      messize;
    FTPServ*    ft;
    int         idclient =10;
    FTPattr     atr;

    Tree*      login;
    TreeIter    iterlogin;
    struct userlogin    user;
    struct userlogin*         result;

    ft = FTPServerNew("login");

    atr.m_ft = ft;
    atr.m_sv = ft->m_gs;
    NewClient(idclient,&atr);

    login = atr.m_ft->m_userlogin;

    strcpy(user.m_username,"serj");
    iterlogin = TreeFind(login,&user);
    if (! (TreeIsEmpty(login)) )
    {

        if (! (iterlogin == TreeEnd (login)) )
        {
            ++flag;
        }
    }

    result =(struct userlogin*)TreeGetData(iterlogin);

    if(!(strcmp(result->m_username,"serj")) )
    {
        ++flag;
    }

    memcpy(headbuf+sizeof(FTPHead),mes,strlen(mes)+1);
    FTPFillHeadr2(headbuf,0,1,0,strlen(mes), COMND_M);
    messize = Headrfunc(headbuf,idclient,&atr);

    /*Right command and right user name*
    NewMessage(idclient,&atr,mes,strlen(mes)+1);

    if(atr.m_cl->m_state == PASS_S)
    {
       ++flag;
    }

    /*Wrong command and right user name*
    atr.m_cl->m_state = USER_S;
    NewMessage(idclient,&atr,"Userr serj",strlen("Userr serj")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

/*More that 2 parameters in command line*
    NewMessage(idclient,&atr,"User User serj",strlen("User User serj")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

/*Right command wrong user name*
    NewMessage(idclient,&atr,"User chuk",strlen("User chuk")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

    if (flag == 6)
    {
        return 0;
    }

    return 1;
}

int unitest_NewPassword()
{
    /*Produce the new ftp*/
    char        headbuf[BUFSIZE];
    int         flag=0;
    char*       mes="User serj";
    size_t      messize;
    FTPServ*    ft;
    int         idclient =10;
    FTPattr     atr;

    ft = FTPServerNew("login");

    atr.m_ft = ft;
    atr.m_sv = ft->m_gs;
    NewClient(idclient,&atr);

    memcpy(headbuf+sizeof(FTPHead),mes,strlen(mes)+1);
    FTPFillHeadr2(headbuf,0,1,0,strlen(mes), COMND_M);
    messize = Headrfunc(headbuf,idclient,&atr);

    /*Right command and right user name*/
    NewMessage(idclient,&atr,mes,strlen(mes));
    if(atr.m_cl->m_state == PASS_S)
    {
       ++flag;
    }

    /*Wrong Password */
    NewMessage(idclient,&atr,"Pass n234dgh",strlen("Pass n234dgh")+1);
    if(atr.m_cl->m_state == USER_S && atr.m_cl->m_login == 0 )
    {
       ++flag;
    }

/*Wrong Password 2nd time*/
    NewMessage(idclient,&atr,"Pass detr34dgh",strlen("Pass detr34dgh")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }



/*Right user name*/
    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S && atr.m_cl->m_login)
    {
       ++flag;
    }

    /*wrong passwrod*/
    NewMessage(idclient,&atr,"Pass detr34dgh",strlen("Pass detr34dgh")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

    /*Right user name*/
    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S  && atr.m_cl->m_login->m_logcount == 2)
    {
       ++flag;
    }

  /*Righth Path */
    NewMessage(idclient,&atr,"Pass  mucha",strlen("Pass  mucha")+1);
    if(atr.m_cl->m_state == WAIT_S)
    {
       ++flag;
    }



    /*Testing the Denied access*/
    atr.m_cl->m_state = USER_S;

    /*Right user name*/
    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S  && atr.m_cl->m_login->m_logcount == 0)
    {
       ++flag;
    }

    /*Bad Path */
    NewMessage(idclient,&atr,"Pass  mu",strlen("Pass  mucha")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S  && atr.m_cl->m_login->m_logcount == 1)
    {
       ++flag;
    }

    /*Bad Path */
    NewMessage(idclient,&atr,"Pass  mu",strlen("Pass  mucha")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S  && atr.m_cl->m_login->m_logcount == 2)
    {
       ++flag;
    }

    /*Bad Path */
    NewMessage(idclient,&atr,"Pass  mu",strlen("Pass  mucha")+1);
    if(atr.m_cl->m_state == USER_S)
    {
       ++flag;
    }

   if (TreeEnd(atr.m_ft->m_Clients) == TreeFind(atr.m_ft->m_Clients,atr.m_cl))
   {
       ++flag;
   }

    if (flag == 14)
    {
        return 0;
    }

    return 1;
}

int unitest_Bye()
{
    /*Produce the new ftp*/
    char        headbuf[BUFSIZE];
    int         flag=0;
    size_t      messize;
    FTPServ*    ft;
    int         idclient =10;
    FTPattr     atr;

    ft = FTPServerNew("login");

    atr.m_ft = ft;
    atr.m_sv = ft->m_gs;
    NewClient(idclient,&atr);

    if (!TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }
    memcpy(headbuf+sizeof(FTPHead),"Bye",strlen("Bye")+1);
    FTPFillHeadr2(headbuf,0,1,0,strlen("Bye"), COMND_M);
    messize = Headrfunc(headbuf,idclient,&atr);

    /*Right command and right user name*/
    NewMessage(idclient,&atr,"Bye",strlen("Bye")+1);
    if (TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }



    {/*Disconnect after User*/
    NewClient(idclient,&atr);
    if (!TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }
    if (!TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }

    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S)
    {
       ++flag;
    }
    NewMessage(idclient,&atr,"Bye",strlen("Bye")+1);
    if (TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }
    }


    {/*Disconnect after Pass*/
    NewClient(idclient,&atr);
    if (!TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }

    NewMessage(idclient,&atr,"User serj",strlen("User serj")+1);
    if(atr.m_cl->m_state == PASS_S)
    {
       ++flag;
    }
    NewMessage(idclient,&atr,"Pass  mucha",strlen("Pass  mucha")+1);
    if(atr.m_cl->m_state == WAIT_S)
    {
       ++flag;
    }
    NewMessage(idclient,&atr,"Bye",strlen("Bye")+1);
    if (TreeIsEmpty(atr.m_ft->m_Clients) )
    {
         ++flag;
    }
    }

    if (flag == 10)
    {
        return 0;
    }

    return 1;
}

int unitest_Get()
{
    FTPServ*    ft;
    int         idclient =10;
    FTPattr     atr;
    char*       filename={"a"};
    FILE*       fp1;
    FILE*       fp2;
    int         endpos1;
    int         endpos2;


    ft = FTPServerNew("login");

    atr.m_ft = ft;
    atr.m_sv = ft->m_gs;
    atr.m_param = filename;
    NewClient(idclient,&atr);

    FTPUserGet(&atr);

    fp1 = fopen(filename,"r");
    fp2 = fopen("chizh.odt","r");
    fseek (fp1, 0, SEEK_END);
    fseek (fp2, 0, SEEK_END);
    endpos1 =  ftell(fp1);
    endpos2 =  ftell(fp2);
    fclose(fp1);
    fclose(fp2);

    if (endpos1 == endpos2)
    {
        return 0;
    }
    return 1;

}

int unitest_FTPls()
{
    FTPServ*    ft;
    int         idclient =10;
    FTPattr     atr;

    ft = FTPServerNew("login");

    atr.m_ft = ft;
    atr.m_sv = ft->m_gs;
    NewClient(idclient,&atr);
    FTPls(&atr);
    puts("");
    return 0;
}


#endif /* _unitest_ftp_server_*/
